package org.example.controller.userrole;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import liquibase.pro.packaged.A;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.CusRole;
import org.example.entity.CusUser;
import org.example.entity.CusUserRole;
import org.example.mapper.CusRoleMapper;
import org.example.mapper.CusUserMapper;
import org.example.mapper.CusUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/userrole")
public class UserRoleController {

    @Autowired
    private CusUserMapper cusUserMapper;
    @Autowired
    private CusRoleMapper cusRoleMapper;
    @Autowired
    private CusUserRoleMapper cusUserRoleMapper;

    @GetMapping(value = "/test")
    public Object userListGet(){
        return "123";
    }

    @PostMapping(value = "/user/save",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object userSave(@RequestBody JSONObject body){
        CusUser cusUser = JSONUtil.toBean(body,CusUser.class);
        if(cusUser.getId()==null||cusUser.getId().isEmpty()){
            cusUser.setId(IdUtil.randomUUID());
        }
        int insertCount = cusUserMapper.insert(cusUser);

        JSONObject result = new JSONObject();
        result.set("success",insertCount>0);
        result.set("data",cusUser);
        return result;
    }

    @PostMapping(value = "/user/remove",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object userRemove(@RequestBody JSONObject body){
        String userId = body.getStr("id","");
        int deleteCount = cusUserMapper.deleteById(userId);

        JSONObject result = new JSONObject();
        result.set("success",deleteCount>0);
        result.set("id",userId);
        return result;
    }

    @PostMapping(value = "/user/list",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object userList(@RequestBody JSONObject body){
        return cusUserMapper.listUserWithRole();
    }

    @PostMapping(value = "/role/save",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object roleSave(@RequestBody JSONObject body){
        CusRole cusRole = JSONUtil.toBean(body,CusRole.class);
        if(cusRole.getId()==null||cusRole.getId().isEmpty()){
            cusRole.setId(IdUtil.randomUUID());
        }
        int insertCount = cusRoleMapper.insert(cusRole);

        JSONObject result = new JSONObject();
        result.set("success",insertCount>0);
        result.set("data",cusRole);
        return result;
    }

    @PostMapping(value = "/role/remove",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object roleRemove(@RequestBody JSONObject body){
        String roleId = body.getStr("id","");
        int deleteCount = cusRoleMapper.deleteById(roleId);

        JSONObject result = new JSONObject();
        result.set("success",deleteCount>0);
        result.set("id",roleId);
        return result;
    }

    @PostMapping(value = "/role/list",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object roleList(@RequestBody JSONObject body){
        return cusRoleMapper.listRoleWithUser();
    }

    @PostMapping(value = "/userrole/save",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object userRoleSave(@RequestBody JSONObject body){
        CusUserRole cusUserRole = JSONUtil.toBean(body,CusUserRole.class);
        if(cusUserRole.getId()==null||cusUserRole.getId().isEmpty()){
            cusUserRole.setId(IdUtil.randomUUID());
        }
        int insertCount = cusUserRoleMapper.insert(cusUserRole);

        JSONObject result = new JSONObject();
        result.set("success",insertCount>0);
        result.set("data",cusUserRole);
        return result;
    }

    @PostMapping(value = "/userrole/remove",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object userRoleRemove(@RequestBody JSONObject body){
        CusUserRole cusUserRole = JSONUtil.toBean(body,CusUserRole.class);
        int deleteCount = cusUserRoleMapper.delete(new LambdaQueryWrapper<CusUserRole>()
                .eq(CusUserRole::getUserId,cusUserRole.getUserId())
                .eq(CusUserRole::getRoleId,cusUserRole.getRoleId())
        );
        JSONObject result = new JSONObject();
        result.set("success",deleteCount>0);
        return result;
    }


}
